import BaseResponse from './BaseResponse';
import Request from './Request'
import { RequestMethod, ContentType, RequestOptions } from './HttpConfig';

const request = new Request();

interface IParamJoinKeys {
  (params: any): string;
}

const joinParamKey: IParamJoinKeys = (params) => {
  const paramsData = Object.keys(params).map(key => `${key}=${params[key]}`).join('&');
  return paramsData || '';
}

export default class HttpTools {

  /**
   * Get请求
   * */
  Get<T>(url: string, params?: any, headerOptions?: any): Promise<BaseResponse<T>> {

    const joinParam = joinParamKey(params);

    const options: RequestOptions = {
      url: `${url}${joinParam ? `?${joinParam}` : ''}`,
      method: RequestMethod.GET,
      queryParams: joinParam,
      header: {
        'Content-Type': headerOptions.contentType || ContentType.HTTP,
      }
    };
    return request.createHttpRequest(options);
  }
}

export const http = new HttpTools();